package com.wei.web.middleware.retry.sequence;

import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 异步任务处理类
 *
 * @author buhuan.wang
 * @since 2021/12/18
 */
@Service
@Slf4j
public class AsyncTaskManager {

    private ThreadPoolExecutor testPoolExecutor;

    private static final int POOL_THREAD_NUM = 30;

    @PostConstruct
    private void init() {
        testPoolExecutor = new ThreadPoolExecutor(
                POOL_THREAD_NUM,
                POOL_THREAD_NUM,
                0, TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public void execute(Runnable r) {
        testPoolExecutor.execute(RunnableWrapper.of(r));
    }
}
